import func from '@/func/socket.js';
// import { resolve } from 'url';
import Vue from 'vue';
const CLIENT = {
  GET_CLIENT_NAME_LIST: 'GET_CLIENT_NAME_LIST',
  GET_CLIENT_CONTENT_LIST: 'GET_CLIENT_CONTENT_LIST',
  SET_USER: 'SET_USER',
  SET_ROOM_NUMBER: 'SET_ROOM_NUMBER'
};
const state = {
  user: {
    userName: 'undefined'
  },
  roomNumber: '',
  clientNameList: [],
  clientContentList: {}
};
const getters = {
  getUser: state => state.user,
  getClientNameList: state => state.clientNameList,
  getClientContentList: state => state.clientContentList[state.roomNumber]
};
const actions = {
  getClientNameList({ commit, state }) {
    // commit(ROOM_URL.GET_ROOM_LIST, [{ ID: '01', roomName: '01' }]);
  },
  addClientContentList({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      func.addContent({
        userName: state.user.userName,
        content: data,
        roomNumber: state.roomNumber
      });
    });
  },
  getClientContentList({ commit, state }, data) {
    commit(CLIENT.GET_CLIENT_CONTENT_LIST, data);
  },
  setUser({ commit, state }, data) {
    commit(CLIENT.SET_USER, data);
  },
  addRoomName({ commit }, data) {
    return new Promise((resolve, reject) => {
      func.addRoomName({
        user: state.user.userName
      });
    });
  },
  removeRoomName({ commit }, data) {
    return new Promise((resolve, reject) => {
      func.removeRoomName({
        userName: state.user.userName,
        content: data,
        roomNumber: state.roomNumber
      });
    });
  },
  clearClientContentList({ commit }) {
    commit(CLIENT.GET_CLIENT_CONTENT_LIST, false);
  },
  setClientNameList({ commit }, data) {
    commit(CLIENT.SET_ROOM_NUMBER, data);
  }
};
const mutations = {
  [CLIENT.GET_CLIENT_NAME_LIST](state, data) {
    state.clientNameList = data;
  },
  [CLIENT.GET_CLIENT_CONTENT_LIST](state, data) {
    if (data !== false) {
      state.clientContentList[data.roomNumber].push(data);
    } else {
      state.clientContentList = {};
    }
  },
  [CLIENT.SET_USER](state, data) {
    state.user = data;
  },
  [CLIENT.SET_ROOM_NUMBER](state, data) {
    if (state.clientContentList[data] === undefined) {
      Vue.set(state.clientContentList, data, []);
    }
    state.roomNumber = data;
    func.addRoomName({
      userName: state.user.userName,
      content: '进入房间',
      roomNumber: state.roomNumber
    });
  }
};
const client = {
  state,
  getters,
  actions,
  mutations
};
export default client;
